package com.jh.complicate.netty.buffer;

import java.nio.ByteBuffer;

/**
 * @author Administrator
 * @projectName newforward
 * @title ChildBuffer.java
 * @package com.jh.complicate.netty.buffer
 * @description 子缓冲区
 * @date 2019/6/23 18:34
 */
public class ChildBuffer {
    public static void main(String[] args) {
        // 初始化
        ByteBuffer buffer = ByteBuffer.allocate(10);

        // put数据
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }

        // 设置截取的子缓冲区位置
        buffer.position(3);
        buffer.limit(7);
        ByteBuffer child = buffer.slice();

        // 更改子缓冲区的数据
        for (int i = 0; i < child.capacity(); i++) {
            byte b = child.get(i);
            b *= 10;
            child.put(i, b);
        }

        // 重置父缓冲区属性位置
        buffer.position(0);
        buffer.limit(buffer.capacity());

        // 读取数据
        while (buffer.hasRemaining()) {
            System.out.print(buffer.get() + " ");
        }
    }
}
